package com.ihighest.app.infox.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ImageListviewAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.myview.HorizontialListView;

public class ProductInforActivity extends BaseActivity {

	private ActivityTitle title;
	String id;// productID
	TextView productname, productdes;

	HorizontialListView listview;

	JSONArray images;
String content;
	WebView webview;
	private Handler handler;
	private ImageListviewAdapter imageListviewAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_productinfor);

		title = (ActivityTitle) findViewById(R.id.infox_productinfor_Title);
		title.setTitle("产品详情");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleSetting, title.titleRefresh, title.titleOther, title.rightLayout, title.titleMsg);
		productname = (TextView) findViewById(R.id.id_product_title);
		productdes = (TextView) findViewById(R.id.id_product_introduce);
		webview = (WebView) findViewById(R.id.id_product_webview);
		listview=(HorizontialListView) findViewById(R.id.productinfoImglist);
		
		getintentdata();
		getdata();
		 handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				inint();
			}
		};
		

	}

	private void getdata() {

		Api.data("enterpriseProductContent/" + id + ".json", new ApiCallback(this) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getCode() == 100) {

					try {
						productname.setText(result.getData().getString("title"));
						productdes.setText(result.getData().getString("memo"));
						content=result.getData().getString("content");
						
					images = result.getData().getJSONArray("images");// 图片
					webview.loadData(content, "text/html; charset=UTF-8", null);
					handler.sendEmptyMessage(0x123);
						
						
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {

			}
		}, this);

	}
	
	
	

	protected void inint() {
		// TODO Auto-generated method stub
		
		if (images != null && images.length() > 0) {
			listview.setVisibility(View.VISIBLE);
			ArrayList<String> imagelist=new ArrayList<String>();
			for (int i = 0; i < images.length(); i++) {
				try {
					imagelist.add(images.getString(i));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			imageListviewAdapter = new ImageListviewAdapter(this, imagelist);
			
			LayoutParams par = listview.getLayoutParams();
			View view = imageListviewAdapter.getView(0, null, listview);
			view.measure(0, 0);
			par.height = view.getMeasuredHeight();

			listview.setLayoutParams(par);
			
			listview.setAdapter(imageListviewAdapter);
		
		}
	}

	private void getintentdata() {
		id = getIntent().getStringExtra("id");
	}

}
